import request from '../../utils/request';

// 文档分类接口
export interface DocumentCategoryVO {
  id: number;
  name: string;
  description?: string;
  parentId?: number;
  sort: number;
  status: number;
  createTime: Date;
  updateTime: Date;
}

// 文档接口
export interface DocumentVO {
  id: number;
  title: string;
  content: string;
  summary?: string;
  categoryId: number;
  categoryName?: string;
  author: string;
  authorId: number;
  tags?: string[];
  status: number; // 0-草稿 1-已发布 2-已下线
  viewCount: number;
  likeCount: number;
  isTop: boolean;
  publishTime?: Date;
  createTime: Date;
  updateTime: Date;
}

// 分页查询参数
export interface DocumentPageParams {
  pageNo: number;
  pageSize: number;
  title?: string;
  categoryId?: number;
  author?: string;
  status?: number;
  createTime?: Date[];
}

export interface DocumentCategoryPageParams {
  pageNo: number;
  pageSize: number;
  name?: string;
  status?: number;
}

// 分页响应
export interface PageResult<T> {
  list: T[];
  total: number;
}

// 文档分类API
export const DocumentCategoryApi = {
  // 获取分类分页列表
  getCategoryPage: (params: DocumentCategoryPageParams): Promise<PageResult<DocumentCategoryVO>> => {
    return request.get('/document/category/page', { params });
  },

  // 获取分类详情
  getCategory: (id: number): Promise<DocumentCategoryVO> => {
    return request.get(`/document/category/${id}`);
  },

  // 创建分类
  createCategory: (data: Partial<DocumentCategoryVO>): Promise<number> => {
    return request.post('/document/category', data);
  },

  // 更新分类
  updateCategory: (data: DocumentCategoryVO): Promise<void> => {
    return request.put('/document/category', data);
  },

  // 删除分类
  deleteCategory: (id: number): Promise<void> => {
    return request.delete(`/document/category/${id}`);
  },

  // 获取分类树
  getCategoryTree: (): Promise<DocumentCategoryVO[]> => {
    return request.get('/document/category/tree');
  },

  // 更新分类状态
  updateCategoryStatus: (id: number, status: number): Promise<void> => {
    return request.put(`/document/category/${id}/status`, { status });
  }
};

// 文档API
export const DocumentApi = {
  // 获取文档分页列表
  getDocumentPage: (params: DocumentPageParams): Promise<PageResult<DocumentVO>> => {
    return request.get('/document/page', { params });
  },

  // 获取文档详情
  getDocument: (id: number): Promise<DocumentVO> => {
    return request.get(`/document/${id}`);
  },

  // 创建文档
  createDocument: (data: Partial<DocumentVO>): Promise<number> => {
    return request.post('/document', data);
  },

  // 更新文档
  updateDocument: (data: DocumentVO): Promise<void> => {
    return request.put('/document', data);
  },

  // 删除文档
  deleteDocument: (id: number): Promise<void> => {
    return request.delete(`/document/${id}`);
  },

  // 更新文档状态
  updateDocumentStatus: (id: number, status: number): Promise<void> => {
    return request.put(`/document/${id}/status`, { status });
  },

  // 置顶文档
  topDocument: (id: number, isTop: boolean): Promise<void> => {
    return request.put(`/document/${id}/top`, { isTop });
  },

  // 点赞文档
  likeDocument: (id: number): Promise<void> => {
    return request.post(`/document/${id}/like`);
  },

  // 增加浏览量
  increaseViewCount: (id: number): Promise<void> => {
    return request.post(`/document/${id}/view`);
  },

  // 导出文档
  exportDocument: (params: DocumentPageParams): Promise<void> => {
    return request.get('/document/export', { params, responseType: 'blob' });
  }
};

export default {
  DocumentApi,
  DocumentCategoryApi
};